<?php
/**
 * Created by JetBrains PhpStorm.
 * File: ProductsAction.class.php
 * User: Xnfy520@gmail.com
 * Date: 12-3-23
 * Time: 下午7:28
 */


class ProductsAction extends CommonAction{

	//指向分类列表
	function index(){

		$products = D('Products');
		import("ORG.Util.page");
		if(isset($_POST['search'])){
			if(empty($_POST['key'])){
				$this->error('不能为空');
			}else{
				$datas = array();
				switch($_POST['search']){
					case 1 :
						$datas['id'] = $_POST['key'];
						break;
					case 2:
						$datas['name'] = array("like","%{$_POST['key']}%");
						break;
				}
				$datas['categoryid'] = $_GET['cid'];
				$count = $products->where($datas)->count('id');
				if($count<1){
					$this->error('没有该数据');
				}elseif($count<C('PAGENUM')){
					$pagedata = array(1);
				}else{
					$pagedata = array(0,1,2,3,4,5,7,8);
				}
				$list = $products->where($datas)->order('id desc')->select();
				$page = new page($count, C('PAGENUM'));
				$show = $page->fpage($pagedata);
			}
		}else{
			$datas['categoryid'] = $_GET['cid'];
			$count = $products->where($datas)->count();
			if($count<1){
				$this->assign('isshow', 'no');
				$this->assign('nobody', 'no');
			}elseif($count<C('PAGENUM')){
				$pagedata = array(1);
			}else{
				$pagedata = array(0,1,2,3,4,5,7,8);
			}
			$page = new page($count, C('PAGENUM'));
			$show = $page->fpage($pagedata);
			$list = $products->limit($page->getlimitfirst().','.C('PAGENUM'))->where($datas)->order('id desc')->select();
			//	dump($list);
		}
		$this->assign('show', $show);
		$this->assign('list', $list);
		$this->display();

	}

	//指向添加分类页面
	function add(){

		$this->display('add');


	}

	function upload(){
		$this->display();
	}

	function uploads(){

		import("ORG.Net.upload");
		import("ORG.Util.images");
		$upload = new upload(array("filepath"=>"./Public/pcategory/tmp","allowtype"=>array('gif','png','jpg'),"israndname"=>true,"maxsize"=>2000000));
		if($upload->uploadfiles('pic')){
			$img = new image("./Public/pcategory/tmp");
			$imgname =  $upload->getnewname();
			$img->thumb($imgname, 200, 100, 'thumb_', true);
//			$orginfo = getimagesize('./Public/pcategory/'.$imgname);
//			$newinfo = getimagesize('./Public/pcategory/thumb_'.$imgname);
			echo $upload->getnewname();
		}else{
			echo $upload->geterrormsg();
		}

	}

	//添加分类
	function insert(){
		if(isset($_GET['cid']) && !empty($_GET['cid'])){
			$products = D('Products');
			$tmpdir = './Public/pcategory/tmp';
			$cdir = './Public/pcategory';

			if($list = $products ->create()){
					$pcategory = D('Pcategory');
					$dir = $pcategory->where('id='.$list['categoryid'])->getField('path');
				if($products ->add($list)){
					if($dirsrc = opendir($tmpdir)){
						copy($tmpdir.'/'.$list['pic'], $cdir.'/'.$dir.'/'.$list['pic']);
						copy($tmpdir.'/thumb_'.$list['pic'], $cdir.'/'.$dir.'/thumb_'.$list['pic']);
							while($files = readdir($dirsrc)){
								if($files!='.' && $files!='..'){
									$pathfile = $tmpdir.'/'.$files;
									if(is_file($pathfile)){
										unlink($pathfile);
									}
								}
							}
					}
				$this->success('添加产品成功', __APP__.'/Products/index/cid/'.$_GET['cid']);
			}else{
				$this->error('添加产品失败');
			}

			}else{
				$this->error($products->getError());
			}
		}else{
			$this->error('异常操作');
		}
	}

	//删除单元
	function del(){
		if(isset($_GET['cid']) && !empty($_GET['cid'])){
			if(isset($_POST['del']) && !empty($_POST['del'])){

				$products = D('Products');
				$pcategory = D('Pcategory');
				$dir = $pcategory->where('id='.$_GET['cid'])->getField('path');
				foreach($_POST['del'] as $key=>$value){
						$file = $products->where('id='.$value)->getField('pic');
						unlink('./Public/pcategory/'.$dir.'/thumb_'.$file);
						unlink('./Public/pcategory/'.$dir.'/'.$file);
						$num = $products->delete($value);
				}
				if($num>0){
					$this->success('删除产品成功', __APP__.'/Products/index/cid/'.$_GET['cid']);
				}else{
					$this->error('删除产品失败');
				}

			}else{
				$this->error('异常操作');
			}
		}else{
			$this->error('异常操作');
		}
	}

	//指向修改分类
	function edit(){

		if($_GET['cid'] && !empty($_GET['cid'])){
			if($_GET['id'] && !empty($_GET['id'])){
				$pcategory = M('Pcategory');
				$dir = $pcategory->where('id='.$_GET['cid'])->getField('path');
				$products = M('Products');
				$list = $products->find($_GET['id']);
				$this->assign('dir', $dir);
				$this->assign('list', $list);
				$this->display();
			}else{
				$this->error('异常操作');
			}
		}else{
			$this->error('异常操作');
		}


	}

	//修改分类
	function update(){
		if(isset($_POST['cid']) && !empty($_POST['cid'])){
			if(isset($_POST['id']) && !empty($_POST['id'])){
			$products = D('Products');
			$tmpdir = './Public/pcategory/tmp';
			$cdir = './Public/pcategory';
			if($list = $products->create()){
				$pcategory = D('Pcategory');
				$dir = $pcategory->where('id='.$_POST['cid'])->getField('path');
				$file = $products->where('id='.$_POST['id'])->getField('pic');
				if($products->save($list)){
					if($list['pic']!=$file){
						unlink($cdir.'/'.$dir.'/'.$file);
						unlink($cdir.'/'.$dir.'/thumb_'.$file);
					}
					if($dirsrc = opendir($tmpdir)){
						copy($tmpdir.'/'.$list['pic'], $cdir.'/'.$dir.'/'.$list['pic']);
						copy($tmpdir.'/thumb_'.$list['pic'], $cdir.'/'.$dir.'/thumb_'.$list['pic']);
						while($files = readdir($dirsrc)){
							if($files!='.' && $files!='..'){
								$pathfile = $tmpdir.'/'.$files;
								if(is_file($pathfile)){
									unlink($pathfile);
								}
							}
						}
					}
					$this->success('修改产品成功', __APP__.'/Products/index/cid/'.$_POST['cid']);
				}else{
					$this->error('修改产品失败');
				}
			}else{
				$this->error($products->getError());
			}
			}else{
				$this->error('异常操作');
			}
		}else{
			$this->error('异常操作');
		}
	}


	function select(){

		$category = D('Category');
		import("ORG.Util.page");

		if(isset($_POST['search'])){
			if(empty($_POST['key'])){
				$this->error('不能为空');
			}else{
				$datas = array();
				switch($_POST['search']){
					case 1 :
						$datas['id'] = $_POST['key'];
						break;
					case 2:
						$datas['name'] = array("like","%{$_POST['key']}%");
						break;
				}

				if(isset($_SESSION['mid']) && !empty($_SESSION['mid'])){
					if(isset($_SESSION['cid']) && !empty($_SESSION['cid'])){

						$list = $category->where($datas)->order('id desc')->select();
						$count = $category->where($datas)->count('id');
						if($count<1){
							//	$this->assign('isshow', 'no');
							$this->error('没有该分类数据');
						}elseif($count<C('PAGENUM')){
							$pagedata = array(1);
						}else{
							$pagedata = array(0,1,2,3,4,5,7,8);
						}
						$page = new page($count, C('PAGENUM'));

						$show = $page->fpage($pagedata);
						$this->assign('isshow', 'yes');
						$this->assign('nobody', 'yes');

					}else{
						$this->error('异常操作');
					}
				}else{
					$this->error('异常操作');
				}


			}

		}else{

			if(isset($_GET['mid']) && !empty($_GET['mid'])){
				if(isset($_GET['cid']) && !empty($_GET['cid'])){
					$_SESSION['mid'] = $_GET['mid'];
					$_SESSION['cid'] = $_GET['cid'];
					$count = $category->count();
					if($count<C('PAGENUM')){
						$pagedata = array(1);
					}else{
						$pagedata = array(0,1,2,3,4,5,7,8);
					}
					$page = new page($count, C('PAGENUM'));
					$show = $page->fpage($pagedata);
					$list = $category->relation(true)->limit($page->getlimitfirst().','.C('PAGENUM'))->order('id desc')->select();
					//	dump($list);
					if($count==0){
						$this->assign('isshow', 'no');
						$this->assign('nobody', 'no');
					}else{
						$this->assign('isshow', 'yes');
						$this->assign('nobody', 'yes');
					}
				}else{
					$this->error('异常操作');
				}
			}else{
				$this->error('异常操作');
			}


		}

		$this->assign('show', $show);
		$this->assign('list', $list);
		$this->display();



}
}